package com.taritari.journal.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.taritari.journal.entity.dto.SentimentMqDto;
import com.taritari.journal.services.SentimentClassifyService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
 * @author taritari
 * @date 2023-12-03 16:06
 * @description
 */
@Service
@RabbitListener(queues = {"sentiment.direct.queue"}) //队列名
public class SentimentClassifyConsumer {
    @Autowired
    private SentimentClassifyService sentimentClassifyService;
    @RabbitHandler
    public void receiveMessage(String message){
        SentimentMqDto sentimentMqDto = JSONObject.parseObject(message, SentimentMqDto.class);
        System.out.println("==========================接收消息==========================");
        System.out.println(sentimentMqDto);
        sentimentClassifyService.updateSentimentClassify(sentimentMqDto);
        String givenDate = sentimentMqDto.getData();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            LocalDate date = LocalDate.parse(givenDate, formatter);
            LocalDate today = LocalDate.now();

            if (date.equals(today)) {
                System.out.println("是今天");
                String notTag = sentimentClassifyService.getNotTag(sentimentMqDto.getText(),sentimentMqDto.getAuthorId());
                System.out.println(notTag);
            } else {
                System.out.println("不是今天");
            }
        } catch (DateTimeParseException e) {
            System.out.println("The given string is not a valid date.");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
